package com.jinhua.medical.manager.controller;

import com.jinhua.medical.manager.common.BaseController;
import com.jinhua.medical.manager.common.JSONResult;
import com.jinhua.medical.manager.mybatis.dto.SysChineseMedicalPrice;
import com.jinhua.medical.manager.service.BaseAreaService;
import com.jinhua.medical.manager.service.MedicalPriceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 王兴光 on 2018/6/27.
 */
@Controller
@RequestMapping("/area")
public class BaseAreaController extends BaseController {
    @Resource
    private BaseAreaService baseAreaService;
    @Resource
    private MedicalPriceService priceService;

    @ApiOperation("获取区域列表")
    @RequestMapping(value = "/getAreaList",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getAreaList(){
        return new JSONResult("10000","返回区域列表",baseAreaService.findAreaList());
    }

    @ApiOperation("验证区域是否设置过药品信息")
    @RequestMapping(value = "/isExists",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult isExists(String areaId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("flag",priceService.isExists(areaId));
        map.put("areaId",areaId);
        return new JSONResult("10000","验证区域["+areaId+"]是否设置过药品信息",map);
    }

    @ApiOperation("获取指定区域的药品信息列表并分页")
    @RequestMapping("/areaMedicalList")
    public String areaMedicalList(SysChineseMedicalPrice sysChineseMedicalPrice, Model model){
        priceService.medicalList(sysChineseMedicalPrice,pager);
        model.addAttribute("pager",pager);
        return "frags/medical_areaMedical_list_frag";
    }

    @ApiOperation("初次为某个区域导入药品信息数据")
    @RequestMapping(value = "/improtTemplateData",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult improtTemplateData(String areaId,String medicalUUID,String priceArr,String remarkArr){
        // 验证该区域是否设置过药品信息
        if(!priceService.isExists(areaId)){
            // 未设置过药品信息，添加药品信息模板列表与当前地区绑定
            priceService.initTemplate(areaId,medicalUUID,priceArr,remarkArr);
        }

        return  new JSONResult("10000","为区域["+areaId+"]导入药品信息数据","");
    }

    @ApiOperation("批量更新指定区域的 药品信息")
    @RequestMapping(value = "/moreUpdate",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult moreUpdate(String areaId,String priceArr,String remarkArr,String uuidArr){
        // 更新当前页面的药品信息
        priceService.moreUpdate(uuidArr,priceArr,remarkArr);
        return  new JSONResult("10000","为区域["+areaId+"]更新药品信息","");
    }

}
